package com.murphy.algorithm.likou;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成（）组合
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/24 15:14
 */
public class GenerateOne {

    public List<String> genP(int n){
        List<String> list = new ArrayList<>();
        gen("",list,n,n);
        return list;
    }
    public void gen1(String result,List<String> list, int r, int l){
        //用完返回
        if (l == 0 && r == 0){
            list.add(result);
            return;
        }
        //左括号还有就可以用，用完-1
        if (l > 0){
            gen(result+"(",list,r,l-1);
        }
        //右括号比左括号剩余多就可以用，用完-1
        if (l < r){
            gen(result+")",list,r-1 , l);
        }
    }
    public void gen(String result,List<String> list, int l, int r) {
        if (l == 0 && r == 0){
            list.add(result);
            return;
        }
        if (l > 0){
            gen(result+"(",list,l-1,r);
        }
        if (l < r){
            gen(result+")",list,l,r-1);
        }
    }
    public static void main(String[] args) {
        GenerateOne generateOne = new GenerateOne();
        List<String> list = generateOne.genP(2);
        System.out.println(JSON.toJSON(list));
    }
}
